package com.mxpio.boot.security.api.service.impl;

import com.mxpio.framework.jpa.JpaUtil;
import com.mxpio.framework.security.base.entity.UserProfile;
import com.mxpio.framework.security.base.service.base.BaseServiceImpl;
import com.mxpio.framework.security.base.service.UserProfileService;
import com.mxpio.framework.security.base.util.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service("mxpio.security.userProfileService")
public class UserProfileServiceImpl extends BaseServiceImpl<UserProfile> implements UserProfileService {

    @Override
    @Transactional(readOnly = true)
    public List<UserProfile> listByPageKey(String pageKey) {
        return JpaUtil.linq(UserProfile.class).equal("pageKey", pageKey).equal("userId", SecurityUtils.getLoginUsername()).list();
    }

    @Override
    public List<UserProfile> listByLoginUser() {
        String username = SecurityUtils.getLoginUsername();
        if(username != null) {
            return JpaUtil.linq(UserProfile.class).equal("userId", username).list();
        }
        return Collections.emptyList();
    }
}
